<template>
<q-layout view="hHh Lpr fFf">
    <!-- Be sure to play with the Layout demo on docs -->

    <!-- (Optional) The Header -->
    <q-header :class="isShow?'back-dark shadow-7':'transparent'">
        <q-toolbar class="transparent " style="width: 100%; height: 50px">
            <q-btn @click="handleBack" flat round icon="mdi-keyboard-backspace"></q-btn>
            <!-- <q-img v-if="isShow" src="icons/一方映画logo200708竖版亮色.png" :width="$q.platform.is.mobile ? '150px' : '200px'" contain /> -->
            <q-space></q-space>

            <q-tabs dense shrink stretch v-model="tab" class="text-grey  shadow-7 full-height" active-color="primary" indicator-color="primary" inline-label align="justify" narrow-indicator @input="handleTabChange">
                <q-tab name="shot" label="摄制" icon="mdi-video-box" />
                <q-tab name="monitor" label="后期" icon="mdi-monitor" />

            </q-tabs>

            <q-space></q-space>

            <q-btn flat dense :class="isShow?'':'shadow-7'" icon="mdi-format-list-bulleted" @click="leftDrawer = !leftDrawer" />
            <!-- <q-bar dark style="height: 60px" class="transparent">

                    <q-space />

                    <q-btn flat dense  @click="handleMinWin" :class="isShow?'':'shadow-7'" icon="mdi-minus" class="items-center">

                        <q-tooltip>窗口还原(Esc)</q-tooltip>
                    </q-btn>
                    <q-btn flat dense :class="isShow?'':'shadow-7'"  icon="crop_square" @click="handleMaxWin">
                        <q-tooltip>窗口最大化</q-tooltip>
                    </q-btn>
                    <q-btn flat dense :class="isShow?'':'shadow-7'"  icon="mdi-format-list-bulleted" @click="leftDrawer = !leftDrawer" />

                </q-bar> -->
            <!-- <q-btn flat round dense icon="menu" class="mobile-only q-mr-sm" /> -->
        </q-toolbar>
    </q-header>
    <q-drawer @show="handleChildPageWidth" @hide="handleChildPageWidth" v-model="leftDrawer" :overlay="!$platform.inDesktop()" :width="300" :breakpoint="100" side="left" class="shadow-7" content-class="bg-dark " :behavior="!$platform.inDesktop()?'mobile':'default'">
        <!-- QScrollArea is optional -->
        <q-scroll-area style="height: calc(100% - 80px); margin-top: 80px" :thumb-style="{
            right: '4px',
            borderRadius: '5px',
            backgroundColor: '#009688',
            width: '5px',
            opacity: 0.75,
          }" :bar-style="{
            right: '2px',
            borderRadius: '9px',
            backgroundColor: '#03a9f4',
            width: '9px',
            opacity: 0.2,
          }" class="q-px-md ">

            <q-list v-if="tab=='shot'" class="text-teal-3">

                <q-item class="column q-mt-sm q-pa-none q-mb-sm shadow-7 " clickable v-for="item,index in deviceList" :key="index" @click="handleDeviceSelect(item,index)" :active="curDevice==item.deviceId" v-ripple active-class="bg-teal-9 text-white">
                    <q-item-section :class="curDevice==item.deviceId?'':'bg-grey-9'">
                        <q-card class=" q-pa-sm bg-transparent  column item-center">
                            <tips-title :isPad="false" :title="item.label" :tipStyle="'background:'+item.color +'!important'" lineColor="grey-6"></tips-title>
                            <div class="q-py-sm row items-center  justify-end ">

                                <div class="row flex-center">
                                    <q-badge v-if="item.kind=='audioinput'" outline class="q-pa-sm" color="cyan-4">音频输入</q-badge>
                                    <q-badge v-if="item.kind=='videoinput'" outline class="q-pa-sm" color="cyan-6">视频输入</q-badge>
                                    <q-badge v-if="item.kind=='audiooutput'" outline class="q-pa-sm" color="cyan-6">音频输出</q-badge>
                                </div>
                            </div>

                        </q-card>
                    </q-item-section>

                    <!-- <q-item-section>
                    <q-badge>{{item.stime}}</q-badge>
                </q-item-section>
                <q-item-section>{{item.taskname}}</q-item-section> -->
                </q-item>

                <q-separator />

            </q-list>

        </q-scroll-area>
        <q-card flat class="absolute-top transparent q-py-sm q-px-md q-mt-md" style="height: 80px">
            <div class="bg-transparent row justify-between">
                <div v-if="tab=='shot'" class="row items-center no-wrap justify-between full-width">
                    <div class="text-weight-bold text-h6">
                        通告列表
                        <q-btn flat round size="15px" dense icon="list" @click="leftDrawer = !leftDrawer" />
                    </div>

                    <q-select dark dense outlined v-model="deviceType" @input="handleLoadDeviceList" :options="deviceOptions" map-options emit-value option-label="name" option-value="val" />
                </div>
                <div v-if="tab=='collection'" class="text-weight-bold text-h6">
                    项目收藏
                    <q-btn flat round size="15px" dense icon="list" @click="leftDrawer = !leftDrawer" />
                </div>
                <div v-if="tab=='like'" class="text-weight-bold text-h6">
                    我的喜欢
                    <q-btn flat round size="15px" dense icon="list" @click="leftDrawer = !leftDrawer" />
                </div>

            </div>
            <q-separator color="grey-7" class="q-my-md"></q-separator>

        </q-card>

    </q-drawer>
    <!-- (Optional) The Footer -->
    <q-footer class="row back-dark shadow-up-7 items-center" :style="$platform.inDesktop()?'height:50px':'height:90px'">
        <!-- <q-btn flat round size="15px" dense icon="list" @click="leftDrawer = !leftDrawer" /> -->

    </q-footer>

    <q-page-container class="bg-grey-10">

        <!-- This is where pages get injected -->
        <router-view ref="MonitorContainer" :curTab.sync="tab" />
    </q-page-container>

</q-layout>
</template>

<script>
import 'xgplayer';
import Music from 'xgplayer-music';
import {
    getSeconds
} from "assets/utils/stodate";
import {
    loadMusicType,
} from "assets/api/studio/music"
export default {
    name: 'MonitorLayout',

    props: {
        user: {
            type: Object
        }
    },
    data() {
        return {
            leftDrawer: false,
            pid: '',
            isFull: false,
            isShow: true,
            deviceList: [],
            videoInputList: [],
            audioInputList: [],
            audioOutputList: [],
            curDevice: '',
            tab: 'shot',
            deviceType: 0,
            deviceOptions: [{
                    name: '视频输入',
                    val: 0
                },
                {
                    name: '音频输入',
                    val: 1
                },
                {
                    name: '音频输出',
                    val: 2
                },

            ]

        }
    },
    created() {
        if (this.$platform.inDesktop()) {
            this.leftDrawer = true;
        };
        this.pid = this.$route.params.pid;
        this.projectInfo = this.$store.state.projectinfo;
        this.user = this.$store.state.user;

        // console.log(projectInfo);
    },
    mounted() {
        this.$emit('isSingle', true)
        this.i_name = setInterval(() => {
          this.projectInfo = this.$store.state.projectinfo;
            if (this.projectInfo.name) {
                document.title = this.projectInfo.name + '--监看系统';
                clearInterval(this.i_name)
            }

        }, 500);
        this.handleDeviceList()
        this.handleOnDevice()
    },
    watch: {
        '$q.fullscreen.isActive'(val) {
            // if (val) {
            //     this.isShow = false;
            // } else {
            //     this.isShow = true;
            // }

            console.log(val ? 'In fullscreen now' : 'Exited fullscreen')
        }
    },
    beforeDestroy() {
        this.$emit('isSingle', false)
    },
    methods: {
        handleLoadDeviceList() {
            if (this.deviceType == 0) {
                this.deviceList = this.videoInputList
            }
            if (this.deviceType == 1) {
                this.deviceList = this.audioInputList
            }
            if (this.deviceType == 2) {
                this.deviceList = this.audioOutputList
            }
        },
        handleDeviceList() {
            navigator.mediaDevices.enumerateDevices().then(res => {
                this.audioInputList = [];
                this.videoInputList = [];
                this.audioOutputList = [];
                res.forEach(i => {
                    if (i.kind == 'audioinput') {
                        this.audioInputList.push(i)
                    }
                    if (i.kind == 'videoinput') {
                        this.videoInputList.push(i)
                    }
                    if (i.kind == 'audiooutput') {
                        this.audioOutputList.push(i)
                    }

                })
                this.handleLoadDeviceList()

            }).catch(err => {
                this.$message.error('获取设备列表失败:' + err.message)
            })
        },
        handleOnDevice() {
            navigator.mediaDevices.ondevicechange = (e) => {
                console.log(e, '设备');
                this.$message.warn('检测到有设备变动')
                this.handleDeviceList()

            };

        },
        handleChildPageWidth() {
            this.$refs.MusicListContainer.handlePageWidth()
        },

        handleTabChange(val) {
            console.log('tabchange', val);

        },

        handleDeviceSelect(item, index) {
            console.log(item);

            this.curDevice = item.deviceId
            this.$refs.MonitorContainer.handleLoadVideo(item.deviceId)

        },

        handleBack() {
            this.$router.replace('/studio/' + this.pid)
        },

        handleMinWin() {
            // this.isShow = false;
            this.$q.fullscreen
                .exit()
                .then(() => {
                    // v1.5.0+
                    // success!
                })
                .catch((err) => {
                    // v1.5.0+
                    // oh, no!!!
                });
        },
        handleMaxWin() {
            // this.isShow = false;
            this.$q.fullscreen
                .request()
                .then(() => {
                    console.log(this.$refs);
                })
                .catch((err) => {
                    // v1.5.0+
                    // oh, no!!!
                });
        },

    }
}
</script>
